package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.mat.MathsEntity;
import com.wq.wrongquestion.service.mat.MathsService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/mat")
public class MathsController {
    @Autowired
    public MathsService mathsService;

    @Autowired
    public FileUploadUtils fileUploadUtils;

    final String IMG_PAHT_YT_MAT = "images/mat/yt/";
    final String IMG_PAHT_DA_MAT = "images/mat/da/";
    /************************************************Maths Start************************************************/

    @RequestMapping("/mathsInit")
    public String mathsInit(Model model){
        //章节
        List<String> allZj=mathsService.getMathsZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new MathsEntity());
        
        return "mat/mathsSearch";
    }

    @PostMapping("/mathsIns")
    public String insMaths(MathsEntity mathsEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_MAT);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_MAT);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                mathsEntity.setImgytnm(fileName);
                mathsEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                mathsEntity.setImgdanm(fileName2);
                mathsEntity.setImgdapath(filePath2);
            }
            // 插入数据
            mathsEntity.setFxcs(1);
            mathsEntity.setXueke(mathsEntity.getHidXueke());

            mathsService.insMaths(mathsEntity);

            // return "redirect:/mat/mathsInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/mat/addMaths";
        }
        return "redirect:/mat/mathsInit";
    }
    @RequestMapping("/addMaths")
    public String addMaths(Model model){
        List<String> allZj=mathsService.getMathsZj();
        
        model.addAttribute("allZj", allZj);
        return "mat/addMaths";
    }

    @RequestMapping("/searchMaths")
    public String searchMaths(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, MathsEntity mathsEntity) {

        PageInfo<MathsEntity> maths = mathsService.getMaths(pageNum, pageSize, mathsEntity);
        List<String> allZj=mathsService.getMathsZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("maths", maths);
        model.addAttribute("condEntity", mathsEntity);

        return "mat/mathsSearch";
    }

    @RequestMapping("/searchMathsByID")
    public String searchMathsByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<MathsEntity> mathsByID = mathsService.getMathsByID(id);
        List<String> allZj=mathsService.getMathsZj();
        if(!mathsByID.isEmpty()){
            model.addAttribute("maths", mathsByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "mat/mathsSearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "mat/modifyMaths";
    }

    @PostMapping("/mathsUpd")
    public String mathsUpd(Model model, MathsEntity mathsEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_MAT);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        mathsEntity.setImgytnm(fileName);
                        mathsEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_MAT);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        mathsEntity.setImgdanm(fileName2);
                        mathsEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            mathsEntity.setFxcs(1);
            mathsEntity.setXueke(mathsEntity.getHidXueke());

            Integer updRes=mathsService.updMaths(mathsEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<MathsEntity> mathsByID = mathsService.getMathsByID(mathsEntity.getId());           
            if(mathsByID.isEmpty()){
               mathsByID = new ArrayList<MathsEntity>();
            }
            model.addAttribute("maths", mathsByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "mat/modifyMaths";   
    }

    @PostMapping("/mathsDel")
    public String mathsDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        mathsService.delMaths(id);
        
        return "redirect:/mat/mathsInit";
    }

    /************************************************Maths End************************************************/
}
    
